package com.car.loan.project.tool.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmBankScheme;
import com.car.loan.project.cm.domain.CmCarLicensingAddr;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.service.ICmBankCarTypeService;
import com.car.loan.project.cm.service.ICmBankSchemeService;
import com.car.loan.project.cm.service.ICmBankService;
import com.car.loan.project.cm.service.ICmCarLicensingAddrService;
import com.car.loan.project.cm.service.ICmCarService;
import com.car.loan.project.cm.service.ICmDealerAccountService;
import com.car.loan.project.cm.service.ICmDealerService;
import com.car.loan.project.cm.service.ICmInsuranceCompanyService;
import com.car.loan.project.cm.service.ICmSalesService;
import com.car.loan.project.cm.service.ICmUserAccountService;
import com.car.loan.project.cm.util.select.BankBaseDataSelect;
import com.car.loan.project.cm.util.select.BankCarTypeBaseDataSelect;
import com.car.loan.project.cm.util.select.BankSchemeBaseDataSelect;
import com.car.loan.project.cm.util.select.CarDataSelect;
import com.car.loan.project.cm.util.select.DealerAccountBaseSelect;
import com.car.loan.project.cm.util.select.DealerBaseSelect;
import com.car.loan.project.cm.util.select.InsuranceBaseSelect;
import com.car.loan.project.cm.util.select.SalesBaseDataSelect;
import com.car.loan.project.cm.util.select.UserAccountBaseDataSelect;
import com.car.loan.project.cm.util.vo.SelectedAccountVo;
import com.car.loan.project.cm.util.vo.SelectedCarVo;
import com.car.loan.project.cm.util.vo.SelectedVo;
import com.car.loan.project.system.domain.SysDept;
import com.car.loan.project.system.domain.SysUser;
import com.car.loan.project.system.service.ISysDeptService;
import com.car.loan.project.system.service.ISysUserService;
import com.car.loan.project.xr.util.RedisDataUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/26 13:53
 */
@RestController
@RequestMapping("/tool/base-data")
@Api(value = "基本数据对外接口", tags = "工具接口-基本数据对外接口")
public class BaseDataController {

    @Autowired
    private ICmInsuranceCompanyService insuranceCompanyService;

    @Autowired
    private ICmCarService carService;

    @Autowired
    private ICmCarLicensingAddrService cmCarLicensingAddrService;

    @Autowired
    private ICmDealerService dealerService;

    @Autowired
    private ICmDealerAccountService dealerAccountService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ICmSalesService cmSalesService;

    @Autowired
    private ICmBankService bankService;

    @Autowired
    private ICmBankSchemeService bankSchemeService;

    @Autowired
    private ICmBankCarTypeService bankCarTypeService;

    @Autowired
    private ICmUserAccountService userAccountService;

    @Autowired
    private RedisDataUtil redisDataUtil;

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("dealerList")
    @ApiOperation(value = "获取经销商下拉框", response = SelectedVo.class, responseContainer = "list")
    public AjaxResult getDealerList(@Valid DealerBaseSelect dealerBaseSelect) {
        IPage<CmDealer> page = new Page<>(dealerBaseSelect.getPageNum(),
            dealerBaseSelect.getPageSize());
        return AjaxResult.success(
            dealerService.selectedPage(page, dealerBaseSelect.getName(), dealerBaseSelect.getOf()));
    }

    @PostMapping("reloadRedis")
    @ApiOperation("刷新缓存")
    public AjaxResult reloadRedis() {
        redisDataUtil.reloadRedis();
        return AjaxResult.success();
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("dealerAccountList")
    @ApiOperation(value = "获取经销商账户下拉框", response = SelectedAccountVo.class, responseContainer = "list")
    public AjaxResult getDealerAccountList(@Valid DealerAccountBaseSelect dealerAccountBaseSelect) {
        return AjaxResult.success(dealerAccountService.selectedPage(dealerAccountBaseSelect));
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("carList")
    @ApiOperation(value = "获取车辆品牌信息下拉框", responseContainer = "list", response = SelectedCarVo.class)
    public AjaxResult getCarList(@Valid CarDataSelect carDataSelect) {
        return AjaxResult.success(carService.selectedPage(carDataSelect));
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("salesList")
    @ApiOperation("获取销货单位下拉框")
    public AjaxResult salesList(@Valid SalesBaseDataSelect salesBaseDataSelect) {
        return AjaxResult.success(cmSalesService.selectedPage(salesBaseDataSelect));
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("insuranceCompanyList")
    @ApiOperation("获取保险公司下拉框")
    public AjaxResult getInsuranceCompanyList(@Valid InsuranceBaseSelect insuranceBaseSelect) {
        return AjaxResult.success(insuranceCompanyService.selectedPage(insuranceBaseSelect));
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("/deptBusinessList")
    @ApiOperation(value = "获取业务部门列表", responseContainer = "list", response = SelectedVo.class)
    public AjaxResult deptBusinessList(
        @ApiParam(value = "名称", required = false) @RequestParam(value = "name", required = false) String name,
        @ApiParam(value = "1=搜索正常 2=搜索正常和停用", required = true) @RequestParam("of") Integer of) {

        DeptBusinessList deptBusinessList = new DeptBusinessList();
        deptBusinessList.setName(name);
        deptBusinessList.setOf(of);

        List<SysDept> sysDepts = deptService
            .selectBusinessDeptListByNameAndStatus(deptBusinessList);
        List<SelectedVo> selectedVos = Lists.newArrayList();
        if (StringUtils.isNotEmpty(sysDepts)) {
            sysDepts.forEach(sysDept -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(sysDept.getDeptId()))
                    .setLabel(sysDept.getDeptName()).setStatus(sysDept.getStatus()));
            });
        }
        return AjaxResult.success(selectedVos);
    }

    @Data
    public static class DeptBusinessList {

        private String dataScope;
        private String name;
        private Integer of;
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("/userList")
    @ApiOperation(value = "获取业务员列表", responseContainer = "list", response = SelectedVo.class)
    public AjaxResult userList(
        @ApiParam(value = "名称", required = false) @RequestParam(value = "name", required = false) String name,
        @ApiParam(value = "部门id", required = true) @RequestParam(value = "deptId", required = true) Integer deptId,
        @ApiParam(value = "1=搜索正常 2=搜索正常和停用", required = true) @RequestParam("of") Integer of) {

        AdminBusinessList adminBusinessList = new AdminBusinessList();
        adminBusinessList.setName(name);
        adminBusinessList.setOf(of);
        adminBusinessList.setDepId(deptId);

        List<SysUser> sysUsers = userService.selectUserByUserDataBaseSelect(adminBusinessList);
        List<SelectedVo> selectedVos = Lists.newArrayList();
        if (StringUtils.isNotEmpty(sysUsers)) {
            sysUsers.forEach(sysUser -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(sysUser.getUserId()))
                    .setLabel(sysUser.getUserName())
                    .setStatus(Integer.parseInt(sysUser.getStatus())));
            });
        }
        return AjaxResult.success(selectedVos);
    }

    @Data
    public static class AdminBusinessList {

        private String dataScope;
        private String name;
        private Integer of;
        private Integer depId;
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("userAccountList")
    @ApiOperation(value = "获取业务员账户下拉框", response = SelectedAccountVo.class, responseContainer = "list")
    public AjaxResult userAccountList(@Valid UserAccountBaseDataSelect userAccountBaseDataSelect) {
        return AjaxResult.success(userAccountService.selectedPage(userAccountBaseDataSelect));
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("getAllAccount")
    @ApiOperation(value = "获取所有获取账号信息", response = SelectedAccountVo.class, responseContainer = "list")
    public AjaxResult accountAll(
        @ApiParam("业务员id") @RequestParam(value = "adminId", defaultValue = "0") Long adminId,
        @ApiParam("经销商id") @RequestParam(value = "dealerId", defaultValue = "0") Long dealerId,
        @ApiParam("1=搜索正常 2=搜索正常和停用") @RequestParam(value = "of", defaultValue = "1") Integer of) {
        Map<String, List<SelectedAccountVo>> result = new HashMap<>(2);
        UserAccountBaseDataSelect userAccountBaseDataSelect = new UserAccountBaseDataSelect();
        userAccountBaseDataSelect.setOf(of);
        if (adminId > 0) {
            userAccountBaseDataSelect.setUserId(adminId);
        }
        result.put("adminAccount", userAccountService
            .selectedPage(userAccountBaseDataSelect));

        DealerAccountBaseSelect dealerAccountBaseSelect = new DealerAccountBaseSelect();
        if (dealerId > 0) {
            dealerAccountBaseSelect.setDealerId(dealerId);
        }
        dealerAccountBaseSelect.setOf(of);

        result.put("dealerAccount", dealerAccountService
            .selectedPage(dealerAccountBaseSelect));
        return AjaxResult.success(result);
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("licensingAddrList")
    @ApiOperation(value = "上牌提示信息树接口", response = CmCarLicensingAddr.class)
    public AjaxResult licensingAddrList() {
        return AjaxResult.success(cmCarLicensingAddrService.buildDeptTreeSelect());
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("bankList")
    @ApiOperation(value = "获取按揭银行下拉框", response = SelectedVo.class, responseContainer = "list")
    public AjaxResult bankList(@Valid BankBaseDataSelect bankBaseDataSelect) {
        return AjaxResult.success(bankService.selectedList(bankBaseDataSelect));
    }

    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("bankCarTypeList")
    @ApiOperation(value = "获取按揭银行车辆类型下拉框", response = SelectedVo.class, responseContainer = "list")
    public AjaxResult bankCarTypeList(@Valid BankCarTypeBaseDataSelect bankBaseDataSelect) {
        return AjaxResult.success(bankCarTypeService.selectedList(bankBaseDataSelect));
    }


    //    @PreAuthorize("@ss.hasPermi('tool:base-data:info')")
    @GetMapping("bankSchemeList")
    @ApiOperation(value = "获取按揭银行方案下拉框", response = CmBankScheme.class, responseContainer = "list")
    public AjaxResult bankSchemeList(@Valid BankSchemeBaseDataSelect bankBaseDataSelect) {
        return AjaxResult.success(bankSchemeService.selectedList(bankBaseDataSelect));
    }


}
